<?php
	require("libraries/student.php");
	class Cruises extends Student{
		public function __construct(){
			parent::__construct();
			$this->load->helper(array("url","form"));
			$this->load->library("form_validation");
			$this->load->library("string");	
			$this->load->model("model_tours");
			$this->load->model("model_cruise");	
		}
		public function index(){
			$config['base_url'] = base_url().'admin/cruises/index/';
			$config['total_rows'] = $this->model_cruise->count_all();
			$data['title'] = "Manage cruises";
			$data['template'] = "cruises/list_cruise";
			$config['per_page'] = 10;
			$config['uri_segment'] = 4;
			$config['next_link'] = "Next";
			$config['prev_link'] = "Prev";
			$this->load->library("pagination");
			$this->pagination->initialize($config); 
			$start = $this->uri->segment(4);
			$data['data'] = "";
			$data['act'] = 3;
			$data['listall']= $this->model_cruise->listall($config['per_page'],$start);
			$this->load->view("layout",$data);
		}
		public function add(){
		  	$data['title'] 		= "Add cruise";
			$data['template'] 	= "cruises/add_cruise";
			$data['data']['category'] = $this->model_tours->listcate();
			$data['act'] = 3;
			if($this->input->post("ok") != ""){
				$this->form_validation->set_rules("cate_id","Select category","required");
				if($this->form_validation->run() == FALSE){
					$this->load->view("layout",$data);
				}else{
					$frienly = str_replace(' ', '-', strtolower($this->string->replace($this->input->post('cruise_name'))));
					$db = array(
						"cruise_name" => $this->input->post("cruise_name"),
						"cruise_name_rewrite" => $frienly,
						"cruise_code" => $this->input->post("cruise_code"),
						"cruise_ship" => $this->input->post("cruise_ship"),
						"cruise_star"  => $this->input->post("cruise_star"),
						"cruise_show"  => $this->input->post("cruise_show"),
						"cruise_phone" => $this->input->post("cruise_phone"),
						"cruise_1day"  => $this->input->post("cruise_1day"),
						"cruise_2day"  => $this->input->post("cruise_2day"),
						"cruise_3day"  => $this->input->post("cruise_3day"),
						"cruise_4day"  => $this->input->post("cruise_4day"),
						"cruise_destina" => $this->input->post("cruise_destina"),
						"cruise_add"  => $this->input->post("cruise_add"),
						"cruise_short_info" => $this->input->post("cruise_short_info"),
						"cruise_include" => $this->input->post("cruise_include"),
						"cruise_promo_title" => $this->input->post("cruise_promo_title"),
						"cruise_ltinerary" => $this->input->post("cruise_ltinerary"),
						"cruise_cabins" => $this->input->post("cruise_cabins"),
						"cruise_food" => $this->input->post("cruise_food"),
						"cruise_activities" => $this->input->post("cruise_activities"),
						"cruise_price" => $this->input->post("cruise_price"),
						"cruise_price_old" => $this->input->post("cruise_price_old"),
                        "cruise_info"  => $this->input->post("cruise_info"),
						"cruise_1day_val"  => $this->input->post("cruise_1day_val"),
						"cruise_2day_val"  => $this->input->post("cruise_2day_val"),
						"cruise_3day_val"  => $this->input->post("cruise_3day_val"),
						"cruise_4day_val"  => $this->input->post("cruise_4day_val"),
						"cruise_status" => "1",
						"cate_id"     => $this->input->post("cate_id"),
					);
					if($_FILES['cruise_image1']['name'] != NULL){
						$config['upload_path']   = './uploads/cruises';
						$config['allowed_types'] = 'jpg|png|jpeg|gif';
						$config['max_size']	= '2500';
						$config['max_width']  = '2524';
						$config['max_height']  = '2524';
						$config['encrypt_name'] = TRUE;
						$this->load->library('upload',$config);
						if(!$this->upload->do_upload("cruise_image1")){
							$data = array('error' => $this->upload->display_errors());
							$data['title'] 		= "Add cruise";
							$data['template'] 	= "cruises/add_cruise";
							$data['data']['category'] = $this->model_tours->listcate();
							$data['act'] = 3;
							$this->load->view("layout",$data);
							return FALSE;
						}else{
							$data = $this->upload->data();
							$db['cruise_image1'] = $data['file_name'];
							$this->createThumbnail($db['cruise_image1'],"cruises",400,300);
						}
					}
					if($_FILES['cruise_image2']['name'] != NULL){
						$config['upload_path']   = './uploads/cruises';
						$config['allowed_types'] = 'jpg|png|jpeg|gif';
						$config['max_size']	= '2500';
						$config['max_width']  = '2524';
						$config['max_height']  = '2524';
						$config['encrypt_name'] = TRUE;
						$this->load->library('upload',$config);
						if(!$this->upload->do_upload("cruise_image2")){
							$data = array('error' => $this->upload->display_errors());
							$data['title'] 		= "Add cruise";
							$data['template'] 	= "cruises/add_cruise";
							$data['data']['category'] = $this->model_tours->listcate();
							$data['act'] = 3;
							$this->load->view("layout",$data);
							return FALSE;
						}else{
							$data = $this->upload->data();
							$db['cruise_image2'] = $data['file_name'];
							$this->createThumbnail($db['cruise_image2'],"cruises",400,300);
						}
					}
					if($_FILES['cruise_image3']['name'] != NULL){
						$config['upload_path']   = './uploads/cruises';
						$config['allowed_types'] = 'jpg|png|jpeg|gif';
						$config['max_size']	= '2500';
						$config['max_width']  = '2524';
						$config['max_height']  = '2524';
						$config['encrypt_name'] = TRUE;
						$this->load->library('upload',$config);
						if(!$this->upload->do_upload("cruise_image3")){
							$data = array('error' => $this->upload->display_errors());
							$data['title'] 		= "Add cruise";
							$data['template'] 	= "cruises/add_cruise";
							$data['data']['category'] = $this->model_tours->listcate();
							$data['act'] = 3;
							$this->load->view("layout",$data);
							return FALSE;
						}else{
							$data = $this->upload->data();
							$db['cruise_image3'] = $data['file_name'];
							$this->createThumbnail($db['cruise_image3'],"cruises",400,300);
						}
					}
					if($_FILES['cruise_image4']['name'] != NULL){
						$config['upload_path']   = './uploads/cruises';
						$config['allowed_types'] = 'jpg|png|jpeg|gif';
						$config['max_size']	= '2500';
						$config['max_width']  = '2524';
						$config['max_height']  = '2524';
						$config['encrypt_name'] = TRUE;
						$this->load->library('upload',$config);
						if(!$this->upload->do_upload("cruise_image4")){
							$data = array('error' => $this->upload->display_errors());
							$data['title'] 		= "Add cruise";
							$data['template'] 	= "cruises/add_cruise";
							$data['data']['category'] = $this->model_tours->listcate();
							$data['act'] = 3;
							$this->load->view("layout",$data);
							return FALSE;
						}else{
							$data = $this->upload->data();
							$db['cruise_image4'] = $data['file_name'];
							$this->createThumbnail($db['cruise_image4'],"cruises",400,300);
						}
					}
					$this->model_cruise->add($db);
					redirect(base_url()."admin/cruises ");
				}
			}else{
				$this->load->view("layout",$data);
			}
		}
		public function update(){
			$id = $this->uri->segment(4);
		  	$data['title'] 		= "Edit cruise";
			$data['template'] 	= "cruises/edit_cruise";
			$data['data']['category'] = $this->model_tours->listcate();
			$data['act'] = 3;
			$data['getdata'] 	=  $this->model_cruise->getdata($id);
			if($data['getdata'] == NULL){
				die();
			}
			$data['stt'] = $data['getdata']['cate_id'];
			if($this->input->post("ok") != ""){
				$this->form_validation->set_rules("cate_id","Select category","required");
				if($this->form_validation->run() == FALSE){
					$this->load->view("layout",$data);
				}else{
					$frienly = str_replace(' ', '-', strtolower($this->string->replace($this->input->post('cruise_name'))));
					$db = array(
						"cruise_name" => $this->input->post("cruise_name"),
						"cruise_name_rewrite" => $frienly,
						"cruise_code" => $this->input->post("cruise_code"),
						"cruise_ship" => $this->input->post("cruise_ship"),
						"cruise_star"  => $this->input->post("cruise_star"),
						"cruise_show"  => $this->input->post("cruise_show"),
						"cruise_phone" => $this->input->post("cruise_phone"),
						"cruise_1day"  => $this->input->post("cruise_1day"),
						"cruise_2day"  => $this->input->post("cruise_2day"),
						"cruise_3day"  => $this->input->post("cruise_3day"),
						"cruise_4day"  => $this->input->post("cruise_4day"),
						"cruise_destina" => $this->input->post("cruise_destina"),
						"cruise_add"  => $this->input->post("cruise_add"),
						"cruise_short_info" => $this->input->post("cruise_short_info"),
						"cruise_include" => $this->input->post("cruise_include"),
						"cruise_promo_title" => $this->input->post("cruise_promo_title"),
						"cruise_ltinerary" => $this->input->post("cruise_ltinerary"),
						"cruise_cabins" => $this->input->post("cruise_cabins"),
						"cruise_food" => $this->input->post("cruise_food"),
						"cruise_activities" => $this->input->post("cruise_activities"),
						"cruise_price" => $this->input->post("cruise_price"),
						"cruise_price_old" => $this->input->post("cruise_price_old"),
                        "cruise_info"  => $this->input->post("cruise_info"),
						"cruise_1day_val"  => $this->input->post("cruise_1day_val"),
						"cruise_2day_val"  => $this->input->post("cruise_2day_val"),
						"cruise_3day_val"  => $this->input->post("cruise_3day_val"),
						"cruise_4day_val"  => $this->input->post("cruise_4day_val"),
						"cruise_status" => "1",
						"cate_id"     => $this->input->post("cate_id"),
					);
					if($_FILES['cruise_image1']['name'] != NULL){
						$config['upload_path']   = './uploads/cruises';
						$config['allowed_types'] = 'jpg|png|jpeg|gif';
						$config['max_size']	= '2500';
						$config['max_width']  = '2524';
						$config['max_height']  = '2524';
						$config['encrypt_name'] = TRUE;
						$this->load->library('upload',$config);
						if(!$this->upload->do_upload("cruise_image1")){
							$data = array('error' => $this->upload->display_errors());
							$data['title'] 		= "Edit cruise";
							$data['template'] 	= "cruises/edit_cruise";
							$data['data']['category'] = $this->model_tours->listcate();
							$data['act'] = 3;
							$data['getdata'] 	=  $this->model_cruise->getdata($id);
							$data['stt'] = $data['getdata']['cate_id'];
							$this->load->view("layout",$data);
							return FALSE;
						}else{
							$data = $this->upload->data();
							$db['cruise_image1'] = $data['file_name'];
							$this->createThumbnail($db['cruise_image1'],"cruises",400,300);
						}
					}
					if($_FILES['cruise_image2']['name'] != NULL){
						$config['upload_path']   = './uploads/cruises';
						$config['allowed_types'] = 'jpg|png|jpeg|gif';
						$config['max_size']	= '2500';
						$config['max_width']  = '2524';
						$config['max_height']  = '2524';
						$config['encrypt_name'] = TRUE;
						$this->load->library('upload',$config);
						if(!$this->upload->do_upload("cruise_image2")){
							$data = array('error' => $this->upload->display_errors());
							$data['title'] 		= "Edit cruise";
							$data['template'] 	= "cruises/edit_cruise";
							$data['data']['category'] = $this->model_tours->listcate();
							$data['act'] = 3;
							$data['getdata'] 	=  $this->model_cruise->getdata($id);
							$data['stt'] = $data['getdata']['cate_id'];
							$this->load->view("layout",$data);
							return FALSE;
						}else{
							$data = $this->upload->data();
							$db['cruise_image2'] = $data['file_name'];
							$this->createThumbnail($db['cruise_image2'],"cruises",400,300);
						}
					}
					if($_FILES['cruise_image3']['name'] != NULL){
						$config['upload_path']   = './uploads/cruises';
						$config['allowed_types'] = 'jpg|png|jpeg|gif';
						$config['max_size']	= '2500';
						$config['max_width']  = '2524';
						$config['max_height']  = '2524';
						$config['encrypt_name'] = TRUE;
						$this->load->library('upload',$config);
						if(!$this->upload->do_upload("cruise_image3")){
							$data = array('error' => $this->upload->display_errors());
							$data['title'] 		= "Edit cruise";
							$data['template'] 	= "cruises/edit_cruise";
							$data['data']['category'] = $this->model_tours->listcate();
							$data['act'] = 3;
							$data['getdata'] 	=  $this->model_cruise->getdata($id);
							$data['stt'] = $data['getdata']['cate_id'];
							$this->load->view("layout",$data);
							return FALSE;
						}else{
							$data = $this->upload->data();
							$db['cruise_image3'] = $data['file_name'];
							$this->createThumbnail($db['cruise_image3'],"cruises",400,300);
						}
					}
					if($_FILES['cruise_image4']['name'] != NULL){
						$config['upload_path']   = './uploads/cruises';
						$config['allowed_types'] = 'jpg|png|jpeg|gif';
						$config['max_size']	= '2500';
						$config['max_width']  = '2524';
						$config['max_height']  = '2524';
						$config['encrypt_name'] = TRUE;
						$this->load->library('upload',$config);
						if(!$this->upload->do_upload("cruise_image4")){
							$data = array('error' => $this->upload->display_errors());
							$data['title'] 		= "Edit cruise";
							$data['template'] 	= "cruises/edit_cruise";
							$data['data']['category'] = $this->model_tours->listcate();
							$data['act'] = 3;
							$data['getdata'] 	=  $this->model_cruise->getdata($id);
							$data['stt'] = $data['getdata']['cate_id'];
							$this->load->view("layout",$data);
							return FALSE;
						}else{
							$data = $this->upload->data();
							$db['cruise_image4'] = $data['file_name'];
							$this->createThumbnail($db['cruise_image4'],"cruises",400,300);
						}
					}
					$this->model_cruise->update($db,$id);
					redirect(base_url()."admin/cruises");
				}
			}else{
				$this->load->view("layout",$data);
			}
		}
		public function del(){
			$id = $this->uri->segment(4);
			$data = $this->model_cruise->getdata($id);
			@unlink("uploads/cruises/".$data['cruise_image1']);
			@unlink("uploads/cruises/thumb/".$data['cruise_image1']);
			@unlink("uploads/cruises/".$data['cruise_image2']);
			@unlink("uploads/cruises/thumb/".$data['cruise_image2']);
			@unlink("uploads/cruises/".$data['cruise_image3']);
			@unlink("uploads/cruises/thumb/".$data['cruise_image3']);
			@unlink("uploads/cruises/".$data['cruise_image4']);
			@unlink("uploads/cruises/thumb/".$data['cruise_image4']);
			$this->model_cruise->del($id);
			redirect(base_url()."admin/cruises");
		}
		public function createThumbnail($fileName,$path,$width,$height){
			$this->load->library('image_lib');
			//$this->load->helper('thumbnail_helper');
			$config['image_library'] = 'gd2';
			$config['source_image'] = 'uploads/'.$path.'/'.$fileName;
			$config['new_image']    = 'uploads/'.$path.'/thumb/'.$fileName;
			$config['create_thumb'] = TRUE;
			$config['maintain_ratio'] = TRUE;
			$config['thumb_marker'] = FALSE;
			$config['width'] = $width;
			$config['height'] = $height;
			$this->image_lib->initialize($config); 
			$this->image_lib->resize();
			$this->image_lib->clear();
		}
	}